using System;
using System.Windows.Forms;
using HestiaBackup.Library.ConfigurationManager;

namespace HestiaBackup.Gui.Forms
{
    public partial class EditFtpProject : Form
    {
        private FtpSetting _setting;

        public FtpSetting Setting
        {
            get { return _setting; }
            set { _setting = value; }
        }

        private bool _IsDataCorrect;

        public bool IsDataCorrect
        {
            get { return _IsDataCorrect; }
        }

        public EditFtpProject(FtpSetting ftpSetting)
        {
            _setting = ftpSetting;
            InitializeComponent();
        }

        private void EditFtpProject_Load(object sender, EventArgs e)
        {
            txtName.Text = _setting.FriendlyName;
            txtLocalFolder.Text = _setting.LocalFolder;
            txtLocalDeletedFolder.Text = _setting.LocalDeletedFolder;
            txtPass.Text = _setting.Password;
            txtRemoteFolder.Text = _setting.RemoteFolder;
            txtServer.Text = _setting.Server;
            txtUser.Text = _setting.User;
            txtLogFile.Text = _setting.LogFileName;
            intHour.Value = _setting.ProcessDate.Hour;
            intMinute.Value = _setting.ProcessDate.Minute;
            chkEnabled.Checked = _setting.Enabled;
            chkKeepDeleted.Checked = _setting.KeepDeletedFiles;
            _IsDataCorrect = false;
        }

        private void pickLocalFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fBrowser = new FolderBrowserDialog();
            fBrowser.ShowNewFolderButton = true;

            if (fBrowser.ShowDialog() == DialogResult.OK)
            {
                txtLocalFolder.Text = fBrowser.SelectedPath;
            }
        }

        private void pickLocalDeleted_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fBrowser = new FolderBrowserDialog();
            fBrowser.ShowNewFolderButton = true;

            if (fBrowser.ShowDialog() == DialogResult.OK)
            {
                txtLocalDeletedFolder.Text = fBrowser.SelectedPath;
            }
        }

        private void pickLogFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog sFile = new SaveFileDialog();
            sFile.CheckFileExists = false;
            sFile.AddExtension = true;
            sFile.DefaultExt = ".log";
            sFile.CreatePrompt = true;
            if (sFile.ShowDialog() == DialogResult.OK)
            {
                txtLogFile.Text = sFile.FileName;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool dataValid = true;
            if (txtName.Text.Length == 0)
            {
                errorProvider1.SetError(txtName, "Field required");
                dataValid = false;
            }
            else
            {
                errorProvider1.SetError(txtName, "");
            }

            if (txtLocalFolder.Text.Length == 0)
            {
                errorProvider1.SetError(txtLocalFolder, "Field required");
                dataValid = false;
            }
            else
            {
                errorProvider1.SetError(txtLocalFolder, "");
            }

            if (txtLogFile.Text.Length == 0)
            {
                errorProvider1.SetError(txtLogFile, "Field required");
                dataValid = false;
            }
            else
            {
                errorProvider1.SetError(txtLogFile, "");
            }

            if (txtServer.Text.Length == 0)
            {
                errorProvider1.SetError(txtServer, "Field required");
                dataValid = false;
            }
            else
            {
                errorProvider1.SetError(txtServer, "");
            }

            if (txtUser.Text.Length == 0)
            {
                errorProvider1.SetError(txtUser, "Field required");
                dataValid = false;
            }
            else
            {
                errorProvider1.SetError(txtUser, "");
            }

            if (txtPass.Text.Length == 0)
            {
                errorProvider1.SetError(txtPass, "Field required");
                dataValid = false;
            }
            else
            {
                errorProvider1.SetError(txtPass, "");
            }

            if (txtRemoteFolder.Text.Length == 0)
            {
                errorProvider1.SetError(txtRemoteFolder, "Field required");
                dataValid = false;
            }
            else
            {
                errorProvider1.SetError(txtRemoteFolder, "");
            }

            if (chkKeepDeleted.Checked && txtLocalDeletedFolder.Text.Length == 0)
            {
                errorProvider1.SetError(txtLocalDeletedFolder, "Field required");
                dataValid = false;
            }
            else
            {
                errorProvider1.SetError(txtLocalDeletedFolder, "");
            }

            if (dataValid)
            {
                _setting.FriendlyName = txtName.Text;
                _setting.LocalFolder = txtLocalFolder.Text;
                _setting.LocalDeletedFolder = txtLocalDeletedFolder.Text;
                _setting.Server = txtServer.Text;
                _setting.User = txtUser.Text;
                _setting.Password = txtPass.Text;
                _setting.RemoteFolder = txtRemoteFolder.Text;

                _setting.LogFileName = txtLogFile.Text;
                _setting.ProcessDate = new DateTime(1983, 05, 14, (int) intHour.Value, (int) intMinute.Value, 00);

                _setting.Enabled = chkEnabled.Checked;
                _setting.KeepDeletedFiles = chkKeepDeleted.Checked;

                btnSave.DialogResult = DialogResult.OK;
                _IsDataCorrect = true;
                Close();
            }
        }
    }
}